namespace Web.Page.User
{
    using System;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Text.RegularExpressions;
    using Lx.Web.Common.UI;
    using Lx.Web.Common.ControlsUI;
    using System.Collections;
    using System.IO;
    using System.Drawing.Imaging;

    using live;

    public class MsRetun : System.Web.UI.Page
    {
        protected string Offers = System.Configuration.ConfigurationManager.AppSettings["Offers"].ToString();
        //Name of cookie to use to cache the consent token. 
        //protected string AuthCookie = System.Configuration.ConfigurationManager.AppSettings["AuthCookie"];
        // Initialize the WindowsLiveLogin module.
        protected static readonly WindowsLiveLogin wll = new WindowsLiveLogin(true);
        protected WindowsLiveLogin.ConsentToken Token;
        protected static readonly DateTime ExpireCookie = DateTime.Now.AddYears(-10);
        protected static readonly DateTime PersistCookie = DateTime.Now.AddYears(10);

        protected bool flag = false;
        protected string p1 = "";
        protected string p2 = "";
        protected override void OnLoad(EventArgs e)
        {
            WindowsLiveLogin.ConsentToken token = null;
            if (Request.Form.Count > 0)
            {
                token = wll.ProcessConsent(Request.Form);
            }

            if (token != null)
            {
                string t = token.Token;
                Token = wll.ProcessConsentToken(t);
                if (Token != null && Token.IsValid())
                {
                    long intlid = Int64.Parse(Token.LocationID, System.Globalization.NumberStyles.HexNumber);
                    flag = true;
                    p1 = intlid.ToString();
                    p2 = Token.DelegationToken;
                }
            }
        }

        #region old
        /*
        //---------------------------- Windows Live -------------------------
        //Comma-delimited list of offers to be used.
        protected string Offers = System.Configuration.ConfigurationManager.AppSettings["Offers"].ToString();
        //Name of cookie to use to cache the consent token. 
        protected string AuthCookie = System.Configuration.ConfigurationManager.AppSettings["AuthCookie"];
        // Initialize the WindowsLiveLogin module.
        protected static readonly WindowsLiveLogin wll = new WindowsLiveLogin(true);
        protected WindowsLiveLogin.ConsentToken Token;
        protected static readonly DateTime ExpireCookie = DateTime.Now.AddYears(-10);
        protected static readonly DateTime PersistCookie = DateTime.Now.AddYears(10);

        protected bool flag = false;
        protected string p1 = "";
        protected string p2 = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie authCookie = null;
            WindowsLiveLogin.ConsentToken token = null;
            if (Request.Form.Count > 0)
            {
                token = wll.ProcessConsent(Request.Form);
                authCookie = new HttpCookie(AuthCookie);
                // If a consent token is found, store it in the cookie and then 
                // redirect to the main page.
                if (token != null)
                {
                    authCookie.Value = token.Token;
                    authCookie.Expires = PersistCookie;
                }
                else
                {
                    authCookie.Expires = ExpireCookie;
                }
                Response.Cookies.Add(authCookie);
                
                return;
            }
            else
            {
                authCookie = Request.Cookies[AuthCookie];
            }

            // If the raw consent token has been cached in a site cookie, attempt to
            // process it and extract the consent token.
            if (authCookie != null)
            {
                string t = authCookie.Value;
                Token = wll.ProcessConsentToken(t);

                if (Token != null && !Token.IsValid())
                {
                    Token = null;
                }
                else if (Token != null)
                {
                    //locationID to signed lid
                    long intlid = Int64.Parse(Token.LocationID, System.Globalization.NumberStyles.HexNumber);
                    flag = true;
                    p1 = intlid.ToString();
                    p2 = Token.DelegationToken;
                    //GoUrl = string.Format("http://www.italki.com/user/ActiveComplete.htm?intlid={0}&token={1}&setp=1", intlid, Token.DelegationToken);
                }
            }
            //Response.Write("http://www.italki.com/");
        }
        */
        #endregion
    }//
}//